////////////////////////////////////////////////////////////
// Workfile : Testdriver.cpp
// Author : Thomas Pfeiffer
// Date : 31.10.2012
// Description : 
////////////////////////////////////////////////////////////
#include <iostream>
#include <cassert>
#include "AddressManager.h"
#include "PersonReader.h"
#include "AddressReader.h"
#include "AsciiWriter.h"
#include "HtmlWriter.h"

int main() {
	//new AddressManager
	AddressManager adrMan;
	//readers and writers
	PersonReader persReader;
	AddressReader adrReader;
	AsciiWriter ascWriter;
	HtmlWriter htmWriter;
	
	try{
		//read files (persons and addresses (ascii))
		adrMan.ReadFile("pers.txt", &persReader);
		std::cout << "ReadFile: read file pers.txt" << std::endl;
		adrMan.ReadFile("pers2.txt", &persReader);
		std::cout << "ReadFile: read file pers2.txt" << std::endl;
		adrMan.ReadFile("adr.txt", &adrReader);
		std::cout << "ReadFile: read file adr.txt" << std::endl;
		adrMan.ReadFile("pers3.txt", &persReader);
		std::cout << "ReadFile: read file pers3.txt" << std::endl;
		//link addresses
		std::cout << "LinkAddresses: linking addresses..." << std::endl;
		adrMan.LinkAddresses();
		//write files (html and ascii)
		std::cout << "WriteAddresses: write file to addresses.txt" << std::endl;
		adrMan.WriteAddresses("addresses.txt", &ascWriter);
		std::cout << "WriteAddresses: write file to addresses.html" << std::endl;
		adrMan.WriteAddresses("addresses.html", &htmWriter);
	}
	catch(char const* ex){
		assert(ex != 0);
		std::cerr << ex << std::endl;
	}

	//file with errors
	try{
		//empty file
		adrMan.ReadFile("errorfile.txt", &persReader);
	}
	catch(char const* ex){
		assert(ex != 0);
		std::cerr << ex << std::endl;
	}

	try{
		//file with only numbers (tested with addressReader)
		adrMan.ReadFile("errorfile2.txt", &adrReader);
	}
	catch(char const* ex){
		assert(ex != 0);
		std::cerr << ex << std::endl;
	}

	try{
		//file with only numbers (tested with personReader)
		adrMan.ReadFile("errorfile2.txt", &persReader);
	}
	catch(char const* ex){
		assert(ex != 0);
		std::cerr << ex << std::endl;
	}

	try{
		//missing index in person file)
		adrMan.ReadFile("errorfile_pers.txt", &persReader);
	}
	catch(char const* ex){
		assert(ex != 0);
		std::cerr << ex << std::endl;
	}

	try{
		//false grammar (string is not a number)
		adrMan.ReadFile("errorfile_adr.txt", &adrReader);
	}
	catch(char const* ex){
		assert(ex != 0);
		std::cerr << ex << std::endl;
	}

	try{
		//not existing file
		adrMan.ReadFile("x.txt", &adrReader);
	}
	catch(char const* ex){
		assert(ex != 0);
		std::cerr << ex << std::endl;
	}

	return 0;
}